package util

import (
	"gobase/app/model"
	"strconv"
	"strings"
)

//Option结构
type SelectOption struct {
	Value string
	Text  string
}

func newSelectOption(value string, text string) *SelectOption {
	return &SelectOption{Value: value, Text: text}
}

//【资源】树形options
func GetOptionsTree(pidStr string, list []*model.Resource) []*SelectOption {
	var optionList = []*SelectOption{}
	c := " ├ "
	for _, v := range list {
		value := strconv.Itoa(v.Id)
		title := v.Title
		title = strings.Repeat(c, int(v.Level)) + title

		if pidStr == strconv.Itoa(v.ParentId) && !inOptionList(value, optionList) {
			optionList = append(optionList, newSelectOption(value, title))
			subOptionList := GetOptionsTree(value, list)
			if len(subOptionList) > 0 {
				optionList = append(optionList, subOptionList...)
			}
		}
	}

	return optionList
}

func inOptionList(value string, slice []*SelectOption) bool {
	result := false
	for _, v := range slice {
		if v.Value == value {
			result = true
			break
		}
	}
	return result
}

//部门树 options
func GetOrgOptionsTree(pidStr string, list []*model.Org) []*SelectOption {
	var optionList = []*SelectOption{}
	c := " ├ "
	for _, v := range list {
		value := strconv.Itoa(v.Id)
		title := v.Name
		title = strings.Repeat(c, int(v.Level)) + title

		if pidStr == strconv.Itoa(v.ParentId) && !inOptionList(value, optionList) {
			optionList = append(optionList, newSelectOption(value, title))
			subOptionList := GetOrgOptionsTree(value, list)
			if len(subOptionList) > 0 {
				optionList = append(optionList, subOptionList...)
			}
		}
	}

	return optionList
}
